package minderupt.selenate.reporting.html;

import minderupt.selenate.core.ExecutionResult;
import minderupt.selenate.core.TestCase;
import minderupt.selenate.core.TestStep;
import minderupt.selenate.dependency.DependencyManager;
import minderupt.selenate.reporting.ReportWriter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * Author:  Michael Dowling <michael.dowling@minderupt.com>
 */
public class HTMLReportWriter implements ReportWriter {

    private static Log LOGGER = LogFactory.getLog(HTMLReportWriter.class);

    private Properties commandMessageMap = new Properties();
    

    public HTMLReportWriter() {

        try {
            commandMessageMap.load(this.getClass().getResourceAsStream("/selenium-command-map.properties"));
        } catch(Exception e) {
            LOGGER.error("Unable to load selenium command map properties.", e);   
        }

    }


    public void writeReport(String file, ExecutionResult result) {

        StringBuilder report = new StringBuilder();
        report.append("<html><body>");
        report.append("<h1>Test Results</h1>");

        List<TestCase> testCaseList = result.getTestCases();
        for(TestCase testCase : testCaseList) {

            

            report.append("<br /><p /><br />");
            report.append("<table border=1 width=640>");
            report.append("<tr><td colspan=3>Test case: " + testCase.getName() + " (" + testCase.getDuration() + "ms) </td></tr>");
            report.append("<tr><td width=175><b><u>Step</u></b></td><td width=265><b><u>Description</u></b></td><td width=200><b><u>Result</u></b></td></tr>");

            for(TestStep step : testCase.getTestSteps()) {

                String bgcolor = "green";
                if(step.getStatus().equals("DESCRIBE")) {
                    bgcolor = "gray";
                } else if(step.getStatus().equals("OK")) {
                    bgcolor = "green";
                } else if(step.getStatus().equals("FAILURE")) {
                    bgcolor = "red";
                } else if(step.getStatus().equals("NOT PERFORMED")) {
                    bgcolor = "white";
                }


                if(step.getStatus().equals("DESCRIBE")) {
                    report.append("<tr><td colspan=3 bgcolor=" + bgcolor + ">" + step.getDetailsOfStatus() + "</td></tr>");
                } else {
                    String params = StringUtils.arrayToCommaDelimitedString(step.getParams());

                    String message = translateToDescription(step.getCommandName(), step.getParams());

                    report.append("<tr><td>" + step.getCommandName() + " " + params + "</td><td>" + message + "</td><td bgcolor=" + bgcolor + ">" + step.getStatus() + " " + step.getDetailsOfStatus() + "</td></tr>");
                }
                
            }

            report.append("</table>");

        }

        report.append("</body></html>");
        File reportFile = new File(file);
        if(reportFile.exists()) reportFile.delete();

        try {
            reportFile.createNewFile();
            FileUtils.writeStringToFile(reportFile, report.toString());
        } catch(Exception e) {
            System.out.println("ERROR:  " + e);
            e.printStackTrace();
        }

    }


    private String translateToDescription(String commandName, String...params) {

        String messageTemplate = this.commandMessageMap.getProperty("command." + commandName);
        if(messageTemplate == null) return "";

        if(params != null && params.length > 0) messageTemplate = StringUtils.replace(messageTemplate, "%TARGET%", params[0]);
        if(params != null && params.length > 1) messageTemplate = StringUtils.replace(messageTemplate, "%VALUE%", params[1]);

        return(messageTemplate);


    }


}
